﻿using System;
using System.Collections.Generic;
using System.Text;
using Scripting;

namespace UserScripts
{
    /// <summary>
    /// Script for tradable scenery objects (chests) usable by ALL.
    /// </summary>
    public class SceneryTradePublic : ScriptBase
    {
        
        public void OnInteract(Actor actor, SceneryInstance scenery)
        {
            //First time key is ever set, so make it closed.
            if (!scenery.Globals.ContainsKey("State")
                || scenery.Globals["State"] == null
                || scenery.Globals["State"].Length == 0)
            {
                openForm(scenery, actor);
            }
            else
            {
                PacketReader read = new PacketReader(scenery.Globals["State"]);
                string myState = read.ReadString();
                string holder = read.ReadString();
                if (myState == "CLOSED")
                {
                    openForm(scenery, actor);
                }
                else
                {
                    if (actor.Name == holder)
                        actor.Output(0, "No dupping!!");
                    else
                        actor.Output(0, string.Format("{0} currently has the chest open, only one instance may be open at a time!", holder), System.Drawing.Color.Red);
                }
            }
        }

        private void openForm(SceneryInstance scene, Actor actor)
        {
            // Fairly simple, just show the form dialog, it does the rest
            PacketWriter write = new PacketWriter();
            write.Write("OPEN", true);
            write.Write(actor.Name, true);
            scene.Globals["State"] = write.ToArray();

            SceneryTradeWindow TradeForm = new SceneryTradeWindow();
            TradeForm.SetupWindow(scene);
            actor.CreateDialog(TradeForm);
        }
    }
}
